package javango.polls.forms;

import static javango.contrib.compat.JavangoStatic.Javango;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javango.contrib.compat.AbstractForm;
import javango.contrib.compat.ModelChoiceField;
import javango.contrib.compat.Static;
import javango.db.Manager;
import javango.db.ManagerException;
import javango.db.Managers;
import javango.forms.fields.Field;
import javango.polls.model.Choice;
import javango.polls.model.Poll;

public class VoteForm extends AbstractForm {

	public static class Builder {
		public VoteForm build() {
			return Javango().newForm(VoteForm.class);
		}
	}
	Log log = LogFactory.getLog(VoteForm.class);
	
	public Field choice;
	
	protected void updateChoices(Poll poll) throws ManagerException {
//		Map<String, Object> choices = new HashMap<String, Object>();
//		for (Choice choice : poll.getChoices()) {
//			log.debug("Adding choice: " + choice.toString());
//			choices.put(choice.getId().toString(), choice.toString());
//		}
//		ModelDao<Choice> dao = new ModelDao<Choice>(Choice.class);
//		Map<String, Object> params = new HashMap<String, Object>();
//		params.put("poll__eq", poll);
//		Criteria c = dao.filter(params);
//		choice = new ModelChoiceField(Choice.class).setCriteria(c);
		Manager<Choice> choiceManager = Static.getInjector().getInstance(Managers.class).forClass(Choice.class);
		choice = new ModelChoiceField(Choice.class).setChoices(choiceManager.filter("poll__eq", poll).list());
	}

	public VoteForm(Poll poll) {
		super();
		try {
			updateChoices(poll);
		} catch (ManagerException e) {
			log.error(e,e);
		}
	}

}
